package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw10000204.R;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.geometerplus.android.util.QyreaderPerferencesUtil;

import base.tina.core.task.infc.ITaskResult;
import logic.constants.Const;
import logic.constants.DefaultConsts;


/**
 * @Project： Lqyd_Android
 * @Title：PushSettingPage
 * @Description：推送设置
 * @Author：zhangnannan
 * @Date：2014年8月15日上午10:47
 */

public class PushSettingPage
        extends
        AbstractUIPage<BaseBusinessActivity>
        implements
        OnClickListener
{
	
	boolean                         PushInfoSwitchIsOn;
	boolean                         PushNoticeIsRing;
	boolean                         PushNoticeIsShock;
	
	private QyreaderPerferencesUtil dataUtil;
	private TextView                tvTitle;
	private Button                  btnBack;
	private View                    subLayout;
	private CheckedTextView         ctvInfoPushSwitch, ctvNoticeRing, ctvNoticeShock;
	
	public PushSettingPage(BaseBusinessActivity t) {
		super(t);
		dataUtil = QyreaderPerferencesUtil.getInstance(t, Const.phone_number);
	}
	
	private static PushSettingPage page;
	
	public static PushSettingPage getInstance(BaseBusinessActivity t) {
		if (page == null) page = new PushSettingPage(t);
		return page;
	}
	
	@Override
	public <E extends Activity> View createView(E arg0, int arg1) {
		if (null != curMyView) return curMyView;
		
		initView();
		setListener();
		initSwitch();
		return curMyView;
	}
	
	void initView() {
		curMyView = bActivity.getLayoutInflater().inflate(R.layout.user_info_setting_push_setting, null);
		tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
		tvTitle.setText("推送设置");
		btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		subLayout = curMyView.findViewById(R.id.user_info_setting_push_notice_switch_llay);
		
		ctvInfoPushSwitch = (CheckedTextView) curMyView.findViewById(R.id.user_info_setting_push_info_switch_ctv);
		ctvNoticeRing = (CheckedTextView) curMyView.findViewById(R.id.user_info_setting_push_notice_ring_ctv);
		ctvNoticeShock = (CheckedTextView) curMyView.findViewById(R.id.user_info_setting_push_notice_shock_ctv);
		
	}
	
	void setListener() {
		btnBack.setOnClickListener(this);
		ctvInfoPushSwitch.setOnClickListener(this);
		ctvNoticeRing.setOnClickListener(this);
		ctvNoticeShock.setOnClickListener(this);
	}
	
	@Override
	public int enter(IUIPage<?> arg0) {
		return 0;
	}
	
	@Override
	public int leave(IUIPage<?> arg0) {
		return 0;
	}
	
	@Override
	public void notifyView(int arg0, Bundle arg1) {
	}
	
	@Override
	public void setStatus(int arg0) {
	}
	
	@Override
	public View updateView(int arg0, ITaskResult arg1) {
		return null;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				bActivity.onBackPressed();
				break;
			case R.id.user_info_setting_push_info_switch_ctv:
				updateMessageSwitch();
				//Util.showToast(bActivity, "你点击了消息推送按钮");
				break;
			case R.id.user_info_setting_push_notice_ring_ctv:
				updateRingSwitch();
				//Util.showToast(bActivity, "你点击了通知响铃按钮");
				break;
			case R.id.user_info_setting_push_notice_shock_ctv:
				updateShockSwitch();
				//Util.showToast(bActivity, "你点击了通知震动按钮");
				break;
		}
		sendBroadcast();
	}
	
	private void initSwitch() {
		PushInfoSwitchIsOn = dataUtil.getPushMessage();
		PushNoticeIsRing = dataUtil.getShowNotice();
		PushNoticeIsShock = dataUtil.getShowZhendong();
		
		ctvInfoPushSwitch.setBackgroundResource(PushInfoSwitchIsOn ? R.drawable.user_info_setting_push_btn_on : R.drawable.user_info_setting_push_btn_off);
		ctvNoticeRing.setBackgroundResource(PushNoticeIsRing ? R.drawable.user_info_setting_push_btn_on : R.drawable.user_info_setting_push_btn_off);
		ctvNoticeShock.setBackgroundResource(PushNoticeIsShock ? R.drawable.user_info_setting_push_btn_on : R.drawable.user_info_setting_push_btn_off);
		subLayout.setVisibility(PushInfoSwitchIsOn ? View.VISIBLE : View.GONE);
	}
	
	private void updateMessageSwitch() {
		PushInfoSwitchIsOn = !PushInfoSwitchIsOn;
		ctvInfoPushSwitch.setBackgroundResource(PushInfoSwitchIsOn ? R.drawable.user_info_setting_push_btn_on : R.drawable.user_info_setting_push_btn_off);
		subLayout.setVisibility(PushInfoSwitchIsOn ? View.VISIBLE : View.GONE);
		dataUtil.setPushMessage(PushInfoSwitchIsOn);
		System.out.println("当前消息推送开关：" + dataUtil.getPushMessage());
	}
	
	private void updateRingSwitch() {
		PushNoticeIsRing = !PushNoticeIsRing;
		ctvNoticeRing.setBackgroundResource(PushNoticeIsRing ? R.drawable.user_info_setting_push_btn_on : R.drawable.user_info_setting_push_btn_off);
		dataUtil.setShowNotice(PushNoticeIsRing);
		System.out.println("当前消息推送开关-铃声：" + dataUtil.getShowNotice());
	}
	
	private void updateShockSwitch() {
		PushNoticeIsShock = !PushNoticeIsShock;
		ctvNoticeShock.setBackgroundResource(PushNoticeIsShock ? R.drawable.user_info_setting_push_btn_on : R.drawable.user_info_setting_push_btn_off);
		dataUtil.setShowZhendong(PushNoticeIsShock);
		System.out.println("当前消息推送开关-震动：" + dataUtil.getShowZhendong());
	}
	
	private void sendBroadcast(){
        Intent intent = new Intent(DefaultConsts.PUSHMESSAGE_BROADCAST);  
        intent.putExtra("vibrate", dataUtil.getShowZhendong());  
        intent.putExtra("notice", dataUtil.getShowNotice());  
        intent.putExtra("pushMessage", dataUtil.getPushMessage());  
        bActivity.sendBroadcast(intent); 
	}
}
